import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return new Scaffold(
      appBar: new AppBar(
        title: new Text('Flex'),
      ),
      body: new Column(
        children: <Widget>[
          //Flex的两个子widget按1：2来占据水平空间
          new Flex(
            direction: Axis.horizontal,
            children: <Widget>[
              new Expanded(
                flex: 1,
                child: new Container(
                  height: 30,
                  color: Colors.red,
                ),
              ),
              new Expanded(
                flex: 2,
                child: new Container(
                  color: Colors.green,
                  height: 30,
                ),
              )
            ],
          ),
          new Padding(
            padding: const EdgeInsets.only(top: 20),
            child: new SizedBox(
              height: 100,
              //Flex的三个子widget，在垂直方向按2：1：1来占用100像素的空间
              child: Flex(
                direction: Axis.vertical,
                children: <Widget>[
                  new Expanded(
                    flex: 2,
                    child: new Container(
                      height: 30,
                      color: Colors.red,
                    ),
                  ),
                  Spacer(
                    flex: 1,
                  ),
                  new Expanded(
                    flex: 1,
                    child: new Container(
                      height: 30,
                      color: Colors.green,
                    ),
                  ),
                  new Container(
                    color: Colors.orange,
                    child: new Flex(
                      direction: Axis.horizontal,
                      children: <Widget>[
                        new Text('hellow word'),
                        new Text('hellow word'),
                        new Text('hellow word'),
                        new Text('hellow word'),
                        new Text('hellow word'),
                        new Text('hellow word'),
                        new Text('hellow word'),
                      ],
                    ),
                  )
                ],
              ),
            ),
          )
        ],
      ),
    );
  }
}


//示例中的Spacer的功能是占用指定比例的空间，实际上它只是Expanded的一个包装：
/*
new Expanded(
  flex: flex,
  child: const SizedBox(
    height: 0.0,
    width: 0.0,
  ),
);
*/
